package controller.expertInfo.expertEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertBaseServices;
import validator.EmailFluentValidator;
import validator.MobileFluentValidator;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.expertInfo.ExpertBase;

import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: expert
 */
@Controller

@RequestMapping("expertInfo/expertEdit/expertBase")
public class ExpertSelfBaseController extends BaseController {
    @Autowired
    ExpertBaseServices expertBaseServices;



    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {
        map.put("data", expertBaseServices.expertSelectByPrimaryKey(getLoginAccount().getExpertBase().getId()));
        return "expertInfo/expertEdit/expertBase/index";
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("editDlg")
    public String editDlg(Map map) throws Exception {
        ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(getLoginAccount().getExpertBase().getId());
        map.put(StaticConstant.EDIT_DATA, expertBase);
        map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertBase/update");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        return "expertInfo/expertEdit/expertBase/editDlg";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertBase expertBase) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertBase.getPhone(), new MobileFluentValidator("手机号码"))
                .on(expertBase.getBirthday(), new NotNullFluentValidator("出生日期"))
                .on(expertBase.getSex(), new NotNullFluentValidator("性别"))
                .on(expertBase.getBankCard(), new NotBlankFluentValidator("银行卡号"))
                .on(expertBase.getBankName(), new NotBlankFluentValidator("开户行"))
                .on(expertBase.getEmail(), new EmailFluentValidator("电子邮件"))
                .on(expertBase.getEducation(), new NotNullFluentValidator("教育经历"))
                .on(expertBase.getRetire(), new NotNullFluentValidator("状态"))
                .on(expertBase.getTeacher(), new NotNullFluentValidator("是否老师"))
                .on(expertBase.getOutLander(), new NotNullFluentValidator("是否省外"))
                .on(expertBase.getRank(), new NotNullFluentValidator("职称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }

        //防止数据被篡改
        expertBase.setId(getLoginAccount().getExpertBase().getId());
        expertBase.setName(null);
        expertBase.setIdCard(null);
        expertBase.setLive(null);

        //防止图片被错误覆盖 图标地址长度都大于30
        if (expertBase.getHeadImg() != null && expertBase.getHeadImg().length() < 30) {
            expertBase.setHeadImg(null);
        }
        expertBaseServices.expertUpdateByPrimaryKeySelective(expertBase);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("cancelApply")
    public CommonResult cancelApply(@RequestParam(value = "id",required = false) String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();

        if (StringUtils.isBlank(id)){
            id=getLoginAccount().getExpertBase().getId();
        }
        int result=  expertBaseServices.expertCancelApplyByPrimaryKey(id);
        commonResult.setStatus(result);
        return commonResult;
    }

}
